import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter_lui/flutter_lui.dart';
import 'package:flutter_lui/src/l_style.dart';

/// 显示一个图片
///
/// 可以是网络图片 也可以是本地图片，网络图片需要以http开头
class LImage extends StatelessWidget {
  /// 图片地址
  final dynamic img;

  /// 图片宽度
  final double? width;

  /// 图片高度
  final double? height;

  /// 图片颜色
  final Color? color;

  /// 图片填充模式
  final BoxFit? fit;

  /// 图片圆角
  final double? radius;

  /// 使用圆角的角 默认为[true] 遵循css用法 [左上，右上，右下，左下]
  final List<bool> useRadius;

  /// 点击事件
  final Function()? onTap;

  /// 外边距
  /// - [1]=>all
  /// - [1,1]=>[上下，右左]
  /// - [1,1,1]=> [上，右左，下]
  /// - [1,1,1,1]=> [上，右，下，左]
  final List<double> margin;

  /// 左外边距
  final double? marginLeft;

  /// 右外边距
  final double? marginRight;

  /// 上外边距
  final double? marginTop;

  /// 下外边距
  final double? marginBottom;

  /// 上下外边距
  final double? marginVertical;

  /// 左右外边距
  final double? marginHorizontal;
  const LImage(
      {required this.img,
      this.width,
      this.height,
      this.color,
      this.fit,
      this.radius,
      this.useRadius = const [true],
      this.onTap,
      this.margin = const [0],
      this.marginLeft,
      this.marginRight,
      this.marginTop,
      this.marginBottom,
      this.marginVertical,
      this.marginHorizontal,
      Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    late Widget image;
    if (img is File) {
      image = Image.file(img,
          fit: BoxFit.fill, width: width, height: height, color: color);
    } else if (img is String) {
      if ((img as String).toLowerCase().startsWith('http')) {
        image = Image.network(img,
            frameBuilder: (context, child, frame, wasSynchronouslyLoaded) {
              return frame != null
                  ? child
                  : LText(
                      '加载中...',
                      fontSize: lrpx(10),
                    );
            },
            loadingBuilder: (context, child, loadingProgress) =>
                loadingProgress == null
                    ? child
                    : LText(
                        '加载中...',
                        fontSize: lrpx(10),
                      ),
            errorBuilder: (context, child, loadingProgress) => LText(
                  '加载失败',
                  fontSize: lrpx(10),
                ),
            fit: fit ?? BoxFit.fill,
            width: width,
            height: height,
            color: color);
      } else if (img.isEmpty) {
        image = LBox(
          width: width,
          height: height,
          backgroundColor: color,
          child: '暂无图片',
        );
      } else {
        image = Image.asset(img,
            errorBuilder: (context, child, loadingProgress) => LText(
                  '加载失败',
                  fontSize: lrpx(10),
                ),
            fit: BoxFit.fill,
            width: width,
            height: height,
            color: color);
      }
    } else if (img is Uint8List) {
      image = Image.memory(img,
          errorBuilder: (context, child, loadingProgress) => LText(
                '加载失败',
                fontSize: lrpx(10),
              ),
          fit: fit ?? BoxFit.fill,
          width: width,
          height: height,
          color: color);
    } else {
      image = Image.asset(img,
          errorBuilder: (context, child, loadingProgress) => LText(
                '加载失败',
                fontSize: lrpx(10),
              ),
          fit: BoxFit.fill,
          width: width,
          height: height,
          color: color);
    }
    var radiusFilter = useRadius.where((i) => i == true);
    if (radius != null && radiusFilter.isNotEmpty) {
      var t = findTRBL(useRadius);
      image = ClipRRect(
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(t[0] == true ? radius! : 0),
            topRight: Radius.circular(t[1] == true ? radius! : 0),
            bottomRight: Radius.circular(t[2] == true ? radius! : 0),
            bottomLeft: Radius.circular(t[3] == true ? radius! : 0),
          ),
          child: image);
    }
    if (onTap != null) {
      image = GestureDetector(
        onTap: onTap,
        child: image,
      );
    }
    var marginValue = findTRBL(margin);
    var mt = marginTop ?? marginVertical ?? marginValue[0] ?? 0;
    var mr = marginRight ?? marginHorizontal ?? marginValue[1] ?? 0;
    var mb = marginBottom ?? marginVertical ?? marginValue[2] ?? 0;
    var ml = marginLeft ?? marginHorizontal ?? marginValue[3] ?? 0;
    if (mt != 0 || mr != 0 || mb != 0 || ml != 0) {
      image = Padding(
          padding: EdgeInsets.only(top: mt, right: mr, bottom: mb, left: ml),
          child: image);
    }
    return image;
  }
}
